
package com.jf.cloud.product.controller.app;

import com.jf.cloud.common.constant.Constant;
import com.jf.cloud.common.i18n.I18nMessage;
import com.jf.cloud.common.product.vo.app.CategoryAppVO;
import com.jf.cloud.common.product.vo.app.CategorySearchAppVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.product.service.CategoryService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.Operation;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * 分类信息
 *
 * @author zz
 * @date 2020-10-28 15:27:24
 */
@RestController("appCategoryController")
@RequestMapping("/ua/category")
@Tag(name = "app-分类信息")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private MapperFacade mapperFacade;

    @GetMapping("/category_list")
    @Operation(summary = "获取指定分类下的分类列表（顶级分类的parentId为0,默认为一级分类）" , description = "获取指定分类下的分类列表")
    @Parameters({
            @Parameter(name = "parentId", description = "分类ID" ),
            @Parameter(name = "shopId", description = "店铺id" )
    })
    public ServerResponseEntity<List<CategoryAppVO>> categoryList(@RequestParam(value = "parentId", defaultValue = "0") Long parentId, @RequestParam(value = "shopId", defaultValue = "0") Long shopId) {
        List<CategoryAppVO> categories;
        if (Objects.equals(parentId, 0L) && Objects.equals(shopId, Constant.PLATFORM_SHOP_ID)) {
            categories = categoryService.listPlatformCategoryByShopIdAndParenId(shopId, parentId, I18nMessage.getLang());
        } else {
            categories = categoryService.listByShopIdAndParenId(shopId, parentId, I18nMessage.getLang());
        }
        return ServerResponseEntity.success(mapperFacade.mapAsList(categories, CategoryAppVO.class));
    }

    @GetMapping("/shop_category_list")
    @Operation(summary = "店铺/平台的全部分类列表接口" , description = "店铺/平台分类列表接口")
    @Parameter(name = "shopId", description = "店铺id" )
    public ServerResponseEntity<List<CategoryAppVO>> shopCategoryList(@RequestParam(value = "shopId", defaultValue = "0") Long shopId) {
        List<CategoryAppVO> categories = categoryService.shopCategoryList(shopId, I18nMessage.getLang());
        return ServerResponseEntity.success(categories);
    }


    @GetMapping("/category_search_item")
    @Operation(summary = "分类-搜索参数" , description = "分类-搜索参数")
    @Parameter(name = "categoryId", description = "分类id" , required = true)
    public ServerResponseEntity<CategorySearchAppVO> categorySearchItem(@RequestParam(value = "categoryId") Long categoryId) {
        CategorySearchAppVO  categorySearch = categoryService.categorySearchItem(categoryId);
        return ServerResponseEntity.success(categorySearch);
    }
}
